public class J092701 {
    public static class TreeNode {
      int val;
      TreeNode left;
      TreeNode right;
      TreeNode() {}
      TreeNode(int val) { this.val = val; }
      TreeNode(int val, TreeNode left, TreeNode right) {
          this.val = val;
          this.left = left;
          this.right = right;
      }
    }

    public static TreeNode buildTree(int[] inorder, int[] postorder) {
        int len = postorder.length;
        return build(inorder, postorder, 0, len - 1, 0, len - 1);
    }
    public static TreeNode build(int[] inorder, int[] postorder, int left1, int right1, int left2, int right2){
        if(left1 == right1){
            return new TreeNode(inorder[left1]);
        }

        TreeNode root = new TreeNode(postorder[right2]);
        int key = findNum(inorder, root.val);
        int leftNums = key - left1;

        if(key != left1) {
            root.left = build(inorder, postorder, left1, key - 1, left2, left2 + leftNums - 1);
        }else{
            root.left = null;
        }

        if(key != right1) {
            root.right = build(inorder,postorder, key + 1, right1, left2 + leftNums, right2 - 1);
        }else{
            root.right = null;
        }

        return root;
    }

    public static int findNum(int[] nums, int key){
        int i = 0;
        while(i < nums.length){
            if(nums[i] == key){
                break;
            }
            i++;
        }
        return i;
    }

    public static void main(String[] args) {
        int[] inorder = new int[]{-4,-10,3,-1,7,11,-8,2};
        int[] postorder = new int[]{-4,-1,3,-10,11,-8,2,7};
        TreeNode root = buildTree(inorder, postorder);
        System.out.println(123);
    }
}
